/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.util;

import java.io.File;

/**
 * Utility class for DIRECTORY operations ( set of static methods )
 * 
 * @author Laurent GUERIN
 *
 */
public final class DirUtil
{
    //-----------------------------------------------------------------------------
    /** 
     * Private constructor 
     */
    private DirUtil()
    {
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Checks the existence of a directory
     * @param sDir
     * @return true if the given directory exists and is realy a directory (not a file)
     */
    public static boolean exists( String sDir )
    {
        File file = new File(sDir);
        return file.isDirectory(); // exists and is a directory 
    }
    
    //-----------------------------------------------------------------------------
    /**
     * Creates the given directory, including any necessary but nonexistent parent directories 
     * @param sDir
     * @return true if the directory was created
     */
    public static boolean createDir( String sDir )
    {
        boolean bRet = false ;
        if ( sDir != null )
        {            
		    File dir = new File(sDir);
		    if ( ! dir.exists() )
		    {
			    if ( dir.mkdirs() )
		        {
			        bRet = true ;		            
		        }		
		        else
		        {
		            throw new RuntimeException("createDir() : Cannot create directory '" + sDir + "'" );
		        }
		    }
        }
        return bRet ;
    }

    //-----------------------------------------------------------------------------
    /**
     * Creates the file's directory if it doesn't exist 
     * @param sFile
     * @return true if the directory was created
     */
    public static boolean createFileDir( String sFile )
    {
        boolean bRet = false ;
        if ( sFile != null )
        {            
		    File file = new File(sFile);
		    bRet = createDir( file.getParent() ) ;
        }
        return bRet ;
    }

}